package com.ruoyi.central.controller.vo;

import com.ruoyi.central.domain.*;
import com.ruoyi.common.enums.common.TaskStatusEnum;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.*;

/**
 * @Classname AreaVo
 * @Author: sunluquan
 * @Date: 2022/3/7 14:37
 * @Description TODO
 */
@Data
public class AreaVo implements Serializable{

    public AreaVo() {
    }

    public AreaVo(Integer id,String name, Integer type, @NotEmpty List<TopCentralOperationStation> operationStations) {
        this.name = name;
        this.id = id;
        this.type = type;
        if(operationStations.size()>1){
            operationStations.sort(Comparator.comparing(TopCentralOperationStation::getWeight));
        }
        this.weight = CollectionUtils.getFirst(operationStations).getWeight();

        this.operationStations = new ArrayList<>();

        operationStations.forEach(topCentralOperationStation -> this.operationStations.add(OperationStation.build(topCentralOperationStation)) );
    }
    /** 区域编号 */
    private Integer id;
    /** 区域名称 */
    private String name;

    /** 区域类型 */
    private Integer type;

    /** 区域工位列表 */
    //private List<TopCentralOperationStation> operationStations;

    private List<OperationStation> operationStations;

    /** 区域排序 */
    private int weight;

    @Data
    public static class OperationStation implements Serializable {

        public OperationStation() {
        }

        public OperationStation(Integer id, String name, TaskStatusEnum taskStatus, String materialNo, String batchNo, String processStepName, String processingTime) {
            this.id = id;
            this.name = name;
            this.taskStatus = taskStatus;
            this.materialNo = materialNo;
            this.batchNo = batchNo;
            this.processStepName = processStepName;
            this.processingTime = processingTime;
        }
        private Integer id;//操作工位编号

        private String name;//操作工位名称

        private TaskStatusEnum taskStatus;//操作工位任务状态

        private String materialNo;//操作工位物料编号

        private String batchNo;//操作工位产品批次号

        private String processStepName;//当前工序名称

        private String processingTime;//加工时长



        public static OperationStation build(TopCentralOperationStation topCentralOperationStation){

            TopCentralOperationStationMaterial operationStationMaterial = topCentralOperationStation.getOperationStationMaterial();

            Integer id = topCentralOperationStation.getId();
            String name = topCentralOperationStation.getName();
            String materialNo = null;
            String batchNo = null;
            TaskStatusEnum taskStatus = null;
            String processStepName = null;
            String processingTime = null;
            if(StringUtils.isNotNull(operationStationMaterial)){
                TopCentralMaterial material = operationStationMaterial.getMaterial();
                materialNo = material.getId();
                batchNo = material.getBatchNo();
                if(StringUtils.isNotNull(material.getArtProcessRun())){
                    TopCentralArtProcessRun artProcessRun = material.getArtProcessRun();
                    //当前执行的工序
                    if(StringUtils.isNotNull(artProcessRun.getProcessStepRun())){
                        processStepName = artProcessRun.getProcessStepRun().getName();
                    }
                    taskStatus = artProcessRun.getTaskStatus();
                    if(taskStatus == TaskStatusEnum.NOT_START){
                        processingTime = null;
                    }else if(taskStatus == TaskStatusEnum.EXECUTED_FINISH){
                        processingTime = DateUtils.getDatePoor(artProcessRun.getCreateTime(),artProcessRun.getUpdateTime());
                    }else{
                        processingTime = DateUtils.getDatePoor(artProcessRun.getCreateTime(),DateUtils.getNowDate());
                    }
                }
            }
            return new OperationStation(id,name,taskStatus,materialNo,batchNo,processStepName,processingTime);
        }
    }

    public static AreaVo build( @NotEmpty List<TopCentralOperationStation> operationStations ){

        TopCentralOperationStation operationStation = CollectionUtils.getFirst(operationStations);

        Integer areaId = operationStation.getAreaId();

        TopCentralArea area = operationStation.getArea();
        String areaName = area.getName();
        Integer type = area.getType();

        return new AreaVo(areaId,areaName,type,operationStations);
    }
}
